package com.tecode.user.dao;

import com.tecode.user.pojo.User;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@Component
public class UserDao {

    @Autowired
    private RestTemplate restTemplate;
/*
    @Autowired
    private DiscoveryClient discoveryClient;// Eureka客户端，可以获取到服务实例信息
    */


    public User queryUserById(int id){
        String url = "http://localhost:8081/user/" + id;


       /* List<ServiceInstance> instances = discoveryClient.getInstances("user-service");
        String getUrl ="http://" + instances.get(0).getHost() +":" + instances.get(0).getPort()+"/user/" +id;*/

       String getUrl="http://user-service/user/"+id;

        return this.restTemplate.getForObject(getUrl, User.class);
    }
}